package com.war.service.outofstorage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.war.dao.outOfStorage.ProductOutDao;
import com.war.dao.outOfStorage.ProductReturnDao;
import com.war.pojo.outofstorage.ProductOut;
import com.war.pojo.outofstorage.ProductReturn;
import com.war.pojo.system.Customer;
import com.war.pojo.system.Warehouse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductReturnServiceImpl implements ProductReturnService {
    //创建一个ProductOutDao的实现类对象
    @Autowired(required = false)//括号里的内容可加可不加
    ProductReturnDao productReturnDao;
    @Autowired(required = false)
    ProductOutDao productOutDao;

    @Override
    public HashMap<String, Object> select(ProductReturn pr) {
        HashMap<String, Object> map =  new HashMap<String, Object>();
        //1 设置分页参数
        PageHelper.startPage(pr.getPage(),pr.getRow());
        //2 查询数据库表数据
        List<ProductReturn> list = new ArrayList<>();
        if(pr.getConValue().equals("")&&!pr.getCondition().equals("退货日期范围")){
            list=productReturnDao.select(pr);
        }else {
            if(pr.getCondition().equals("产品名称")){
                list=productReturnDao.selectByProductName(pr);
            }else if(pr.getCondition().equals("客户")){
                list=productReturnDao.selectByClientName(pr);
            }else if(pr.getCondition().equals("退货日期范围")){
                list=productReturnDao.selectByDate(pr);
            }else{
                list=productReturnDao.select(pr);
            }
        }

        //3.把查询的数据转换成分页对象
        PageInfo<ProductReturn> page = new PageInfo<ProductReturn>(list);

        //获取分页的当前页的集合
        map.put("list",page.getList());
        //总条数
        map.put("total",page.getTotal());
        //总页数
        map.put("totalPage",page.getPages());
        //上一页
        if(page.getPrePage()==0){
            map.put("pre",1);
        }else{
            map.put("pre",page.getPrePage());
        }

        //下一页
        //保持在最后一页
        if(page.getNextPage()==0){
            map.put("next",page.getPages());
        }else{
            map.put("next",page.getNextPage());
        }
        //当前页
        map.put("cur",page.getPageNum());

        return map;
    }


    @Override
    public ProductReturn selectByReturnId(ProductReturn pr) {
        return productReturnDao.selectByReturnId(pr);
    }

    @Override
    public String delInfo(ProductReturn pr) {
        int num=productReturnDao.delInfo(pr);
        if(num>0){
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String update(ProductReturn pr) {
        int num=productReturnDao.update(pr);
        if(num>0){
            return "修改成功";
        }
        return "修改失败";
    }

    @Override
    public String addReturn(ProductReturn pr) {
        int num=productReturnDao.addReturn(pr);
        if(num>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public String ifExistWarehousingId(ProductOut po) {
        ProductOut info=productOutDao.selectByOutId(po);
        if(info==null){
            return "出库编号不存在";
        }
        return "入库编号存在";
    }

    @Override
    public int storageNumOver(ProductReturn pr) {
        return productReturnDao.storageNumNow(pr);
    }

    @Override
    public String delAll(List<ProductReturn> pr) {
        int num = 0;
        for(ProductReturn p:pr) {
            num = productReturnDao.delAll(p);
            num++;
        }
        if (num ==pr.size()) {
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public List<Customer> selectClientName() {
        return productReturnDao.selectClientName();
    }

    @Override
    public List<Warehouse> selectWarehouseName() {
        return productReturnDao.selectWarehouseName();
    }

    @Override
    public String getAdministrator(Warehouse warehouse) {
        return productReturnDao.getAdministrator(warehouse);
    }
}
